﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Serialization;

namespace Data
{
    [DataContract]
    public class Song : IPlayable
    {
        #region Объектная модель
        static private readonly Dictionary<Guid, Song> _allSongs = new Dictionary<Guid, Song>();

        static public Song GetById(Guid id)
        {
            if (_allSongs.ContainsKey(id))
            {
                return _allSongs[id];
            }
            else
            {
                return null;
            }
        }
        #endregion

        static private readonly String _tableName = "[songs]";
        static public String TableName
        {
            get { return _tableName; }
        }


        #region Serializeable
        [DataMember]
        private Guid _id;
        public Guid ID
        {
            get { return _id; }
        }

        [DataMember]
        private String _title;
        public String Title
        {
            get { return _title; }
            set { _title = value; }
        }

        [DataMember]
        private String _artist;
        public String Artist
        {
            get { return _artist; }
            set { _artist = value; }
        }

        [DataMember]
        private String _album;
        public String Album
        {
            get { return _album; }
            set { _album = value; }
        }
        #endregion

        public Song(SqlDataReader reader)
        {
             if (null != reader)
             {
                // reader.Read();
                 
                 _id = new Guid((String)reader["id"]);
                 _title = (String)reader["title"];
                 _artist = (String)reader["artist"];
                 _album = (String)reader["album"];

                // reader.Close();
                // не добавляем в _allSongs, т.к. это не нужно на сервере
                //_allSongs.Add(this._id, this);
             }
        }

        public Song(String title, String artist, String album)
        {
            _id = Guid.NewGuid();

            _artist = artist;
            _album = album;
            _title = title;

            _allSongs.Add(this.ID, this);
        }

        public DataTable Converted
        {
            get
            {
                DataSet tmp_ds = new DataSet("ds_" + Song.TableName);
                DataTable tmp_dt = new DataTable(Song.TableName);

                tmp_dt.Columns.Add("id");
                tmp_dt.Columns.Add("title");
                tmp_dt.Columns.Add("album");
                tmp_dt.Columns.Add("artist");

                DataRow tmp_row = tmp_dt.Rows.Add();

                tmp_row["id"] = this._id;
                tmp_row["album"] = this._album;
                tmp_row["title"] = this._title;
                tmp_row["artist"] = this._artist;

                tmp_ds.Tables.Add(tmp_dt);

                return tmp_dt;
            }
        }

        /// <summary>
        /// Метод, который добавляет song в _allSongs после его получения с сервера
        /// </summary>
        /// <param name="song">Вносится в _allSongs</param>
        static public void AddToAllSongs(Song song)
        {
            if (null != song)
            {
                if (!_allSongs.Keys.Contains(song.ID))
                {
                    _allSongs.Add(song.ID, song);
                }
            }
        }

         #region Члены IPlayable

        String IPlayable.Text
        {
            get
            {
                return "\"" + this.Title + "\" from \"" + this.Album + "\" by " + this.Artist;
            }
        }

        IPlayable IPlayable.Next()
        {
            throw new NotImplementedException();
        }

        IPlayable IPlayable.Previous()
        {
            throw new NotImplementedException();
        }

        void IPlayable.Stop()
        {
            throw new NotImplementedException();
        }

        void IPlayable.Pause()
        {
            throw new NotImplementedException();
        }

        public override string ToString()
        {
            return ((IPlayable)this).Text;
        }

         #endregion
    }
}


